package service.state;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import object.StateMess;

import service.CommonServlet;

public class SearchStates extends CommonServlet {
	
	@Override
	protected void execute(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try
		{
			String sql = "select state_mess.s_id,state_mess.s_content,state_mess.s_date,state_mess.p_data,user.u_no,user.u_name from state_mess,user where state_mess.s_content like ? and state_mess.u_no = user.u_no";
			String kword = request.getParameter("kword");
			kword = "%" + kword + "%";
			
			PreparedStatement pstmt = mConnection.prepareStatement(sql);
			pstmt.setString(1, kword);
			//pstmt.setInt(2, uNo);
			ResultSet rs = pstmt.executeQuery();
			List<StateMess> states = new ArrayList<StateMess>();
			while(rs.next())
			{
				StateMess state = new StateMess();
				state.setsId(rs.getInt("state_mess.s_id"));
				state.setsContent(rs.getString("state_mess.s_content"));
				state.setsPic(rs.getString("state_mess.p_data"));
				state.setsDate(rs.getString("state_mess.s_date"));
				state.setuNo(rs.getInt("user.u_no"));
				state.setuName(rs.getString("user.u_name"));
				//state.setuHead(rs.getString("head.h_data"));
				states.add(state);
			}
			sendObject(states, response);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			response.getWriter().println(e.getMessage());
		}
	}

	/**
	 * Constructor of the object.
	 */
	public SearchStates() {
		super();
	}

}
